package game;

import game.classes.RankingPosition;
import java.io.FileNotFoundException;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Font;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
import javax.swing.JLabel;

/**
 * @author Chris
 */
public class Ranking extends JPanel {

    private static final String RANKINGFILE = "Ranking.txt";
    private static final int RANKINGSIZE = 10;
    private Vector rankinglist;

    public Ranking() {
        rankinglist = new Vector();
        readRankingFile();
        sortRanking();
    }

    public boolean registerScoreTo(int score, String name) {
        rankinglist.add(new RankingPosition(name, score));
        sortRanking();
        writeRankingFile();
        return true;
    }

    public JPanel showRanking(final Game game) {
        JPanel panel = new JPanel(null, false);
        JLabel current_label;
        RankingPosition pos;
        int size = rankinglist.size();

        current_label = new JLabel("Ranking");
        current_label.setBounds((Game.FWIDTH - 150) / 2, 20, 200, 40);
        current_label.setFont(new Font("Trebuchet MS", Font.BOLD, 30));
        panel.add(current_label);

        panel.setBackground(Color.GRAY);
        for (int i = 0, ypos = 100; i < size; i++, ypos += 30) {
            pos = (RankingPosition) rankinglist.get(i);
            current_label = new JLabel(pos.getName() + " " + pos.getScore());
            current_label.setBounds(200, ypos, 400, 22);
            current_label.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
            panel.add(current_label);
        }

        JButton exit = new JButton("Volver al menu principal");
        exit.setBounds((Game.FWIDTH - 300) / 2, 500, 300, 70);
        exit.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        game.showMainMenu();
                    }
                });
        panel.add(exit);

        return panel;
    }

    private void sortRanking() {
        RankingPosition current_i, current_j;
        int size = rankinglist.size();
        for (int i = 0; i < size - 1; i++) {
            current_i = (RankingPosition) rankinglist.get(i);
            for (int j = i + 1; j < size; j++) {
                current_j = (RankingPosition) rankinglist.get(j);
                if (current_i.getScore() < current_j.getScore()) {
                    rankinglist.add(i, current_j);
                    rankinglist.add(j, current_i);
                }
            }
        }
    }

    private void readRankingFile() {
        DataInputStream input;
        RankingPosition p;
        try {
            input = new DataInputStream(new BufferedInputStream(new FileInputStream(RANKINGFILE)));
            while (true) {
                try {
                    rankinglist.add(p = new RankingPosition(input.readUTF(), input.readInt()));
                } catch (IOException e) {
                    break;
                }
            }
            try {
                input.close();
            } catch (IOException e) {
                System.out.println("Error al cerrar lectura");
            }
        } catch (FileNotFoundException e) {
        }
    }

    private void writeRankingFile() {
        DataOutputStream output;
        int size;
        RankingPosition currentpos;
        try {
            output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(RANKINGFILE)));
            size = rankinglist.size();
            if (size > RANKINGSIZE) {
                size = RANKINGSIZE;
            }
            for (int i = 0; i < size; i++) {
                currentpos = (RankingPosition) rankinglist.get(i);
                try {
                    output.writeUTF(currentpos.getName());
                    output.writeInt(currentpos.getScore());
                } catch (IOException ex) {
                    System.out.println("Error al escribir posicion:" + i);
                }
            }
            try {
                output.close();
            } catch (IOException ex) {
                System.out.println("Error al cerrar escritura");
            }
        } catch (FileNotFoundException e) {
            System.out.println("No se encontro el archivo de ranking destino");
            System.exit(0);
        }
    }
}
